package Top_Interview_Questions.Dynamic_Programming;

/**
 * @Author: 吕庆龙
 * @Date: 2020/3/4 11:00
 * <p>
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-l-3/
 * 复习的时候一定记得看这篇文章
 */
public class _0121 {
    public static void main(String[] args) {
        _0121 test = new _0121();
        int[] nums = {7,1,5,3,6,4};
        test.maxProfit1(nums);
    }

    /**
     * 这个是官方题解,没有明说是不是动态规划。
     */
    public int maxProfit1(int prices[]) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice)
                minprice = prices[i];
            else if (prices[i] - minprice > maxprofit)
                maxprofit = prices[i] - minprice;
        }
        return maxprofit;
    }




    public int maxProfit(int prices[]) {
        int maxprofit = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                int profit = prices[j] - prices[i];
                if (profit > maxprofit)
                    maxprofit = profit;
            }
        }
        return maxprofit;
    }

}
